package pms.dao;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.Kategorie;
import pms.om.Musikstueck;
import pms.om.Noten;
import pms.om.PersonHatNoten;
import pms.om.Stimme;

public class NotenDao extends HibernateDaoSupport{
	private PersonHatNotenDao personHatNotenDao;
	private MusikstueckDao musikstueckDao;
	private StimmeDao stimmeDao;
	
	/** Getter
	 * @return the personHatNotenDao
	 */
	public PersonHatNotenDao getPersonHatNotenDao() {
		return personHatNotenDao;
	}

	/** Setter
	 * @param personHatNotenDao the personHatNotenDao to set
	 */
	public void setPersonHatNotenDao(PersonHatNotenDao personHatNotenDao) {
		this.personHatNotenDao = personHatNotenDao;
	}

	/** Getter
	 * @return the musikstueckDao
	 */
	public MusikstueckDao getMusikstueckDao() {
		return musikstueckDao;
	}

	/** Setter
	 * @param musikstueckDao the musikstueckDao to set
	 */
	public void setMusikstueckDao(MusikstueckDao musikstueckDao) {
		this.musikstueckDao = musikstueckDao;
	}

	/** Getter
	 * @return the stimmeDao
	 */
	public StimmeDao getStimmeDao() {
		return stimmeDao;
	}

	/** Setter
	 * @param stimmeDao the stimmeDao to set
	 */
	public void setStimmeDao(StimmeDao stimmeDao) {
		this.stimmeDao = stimmeDao;
	}

	/** findById
	 * @param id
	 * @return the Noten
	 */
	public Noten findById (int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Noten) template.get(Noten.class, id);
	}
	
	/** findAll
	 * @return the results
	 */
	@SuppressWarnings("unchecked")
	public List<Noten> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<Noten> results = template.loadAll(Noten.class);
		return results;
	}
	
	/** findPersonHatNotenByNotenId
	 * @param id
	 * @return the foundPersonHatNoten
	 */
	@SuppressWarnings("unchecked")
	public List<PersonHatNoten> findPersonHatNotenByNotenId (int id) {
		HibernateTemplate template = getHibernateTemplate();
		List<PersonHatNoten> allePersonHatNoten = template.loadAll(PersonHatNoten.class);
		List<PersonHatNoten> foundPersonHatNoten = new ArrayList<PersonHatNoten>();
		for (PersonHatNoten personHatNoten : allePersonHatNoten) {
			if (personHatNoten.getNoten().getId() == id) {
				foundPersonHatNoten.add(personHatNoten);
			}
		}
		return foundPersonHatNoten;
	}
	
	/** findPHNByMusikstueckId
	 * @param id
	 * @return the foundPHN
	 */
	@SuppressWarnings("unchecked")
	public List<PersonHatNoten> findPHNByMusikstueckId(int id) {
		HibernateTemplate template = getHibernateTemplate();
		List<PersonHatNoten> allePHN = template.loadAll(PersonHatNoten.class);
		List<PersonHatNoten> foundPHN = new ArrayList<PersonHatNoten>();
		for (PersonHatNoten phn : allePHN) {
			if (phn.getNoten().getMusikstueck().getId() == id) {
				foundPHN.add(phn);
			}
		}
		return foundPHN;
	}
	
	/** create
	 * @param digitalekopie
	 * @param stimme
	 * @param musikstueck
	 */
	public void create(Blob digitalekopie, Stimme stimme, Musikstueck musikstueck) {
		HibernateTemplate template = getHibernateTemplate();
		Noten n = new  Noten(digitalekopie, stimme, musikstueck);
		template.persist(n);
	}
	
	/** modify
	 * @param id
	 * @param digitalekopie
	 * @param stimme
	 * @param musikstueck
	 */
	public void modify(int id, Blob digitalekopie, Stimme stimme, Musikstueck musikstueck) {
		HibernateTemplate template = getHibernateTemplate();
		Noten n = (Noten) template.get(Noten.class, id);
		n.setDigitalekopie(digitalekopie);
		n.setStimme(stimme);
		n.setMusikstueck(musikstueck);
		template.saveOrUpdate(n);
	}
	
	/** deleteById
	 * @param id
	 * @throws Exception NotenDeleteExeption
	 */
	public void deleteById(int id)throws Exception {
		HibernateTemplate template = getHibernateTemplate();
		Noten n = findById(id);
		List<PersonHatNoten> allePersonHatNoten = findPersonHatNotenByNotenId(n.getId());
		if (allePersonHatNoten == null || allePersonHatNoten.size() == 0) {
			template.delete(n);
		} else {
			throw new NotenDeleteExeption();
		}
		template.flush();
	}
}
